/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/03/15
 */
package vn.smartware.co.server.dao.registration;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import vn.smartware.co.dto.MstCustomerDTO;
import vn.smartware.co.framework.bean.LoginDTO;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.server.dao.factory.AbstractRegisterCustomerDAO;
import vn.smartware.co.server.sql.factory.AbstractRegisterCustomerSQLManager;
import vn.smartware.co.server.sql.factory.SQLFactory;

/**
 * @author thuctt
 * RegisterCustomerDAO
 */
public class RegisterCustomerDAO extends AbstractRegisterCustomerDAO {

	private static Log log = LogFactory.getLog(RegisterCustomerDAO.class);

	/**
	 * deleteCustomer
	 * @param customer
	 * @param user
	 * @param conn
	 * @throws ServerException
	 */
	@Override
	public void deleteCustomer(MstCustomerDTO customer, LoginDTO user,
			Connection conn) throws ServerException {

		AbstractRegisterCustomerSQLManager manager = SQLFactory.createRegisterCustomerSQLManager();
		int count = 0;
		try {
			manager.makeDeleteCustomerSQL(customer.getCustomerNo(), user, conn);
			count = manager.executeUpdate();
			log.info(count + " rows deleted.");
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}

	/**
	 * registerCustomer
	 * @param customer
	 * @param user
	 * @param conn
	 * @throws ServerException
	 */
	@Override
	public String registerCustomer(MstCustomerDTO customer, LoginDTO user,
			Connection conn) throws ServerException {

		AbstractRegisterCustomerSQLManager manager = SQLFactory.createRegisterCustomerSQLManager();
		ResultSet rs = null;
		int count = 0;
		String cusNo = "";
		try {
			manager.makeRegisterCustomerSQL(customer, user, conn);
			count = manager.executeUpdate();
			log.info(count + " rows created.");
			//get room no just registered
			manager = SQLFactory.createRegisterCustomerSQLManager();
			manager.getMaxCustomerNoSQL(conn);
			rs = manager.executeQuery();
			if (rs.next()){
				cusNo = rs.getString("CUSTOMER_NO");
			}
			customer.setCustomerNo(cusNo);
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close(rs);
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
		return cusNo;
	}

	/**
	 * updateCustomer
	 * @param customer
	 * @param user
	 * @param conn
	 * @throws ServerException
	 */
	@Override
	public void updateCustomer(MstCustomerDTO customer, LoginDTO user,
			Connection conn) throws ServerException {

		AbstractRegisterCustomerSQLManager manager = SQLFactory.createRegisterCustomerSQLManager();
		int count = 0;
		try {
			manager.makeUpdateCustomerSQL(customer, user, conn);
			count = manager.executeUpdate();
			log.info(count + " rows updated.");
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}
	
}
